package com.lx.zy.framework.security.service;

import com.lx.zy.project.system.domain.SysUser;
import com.lx.zy.project.system.service.ISysMenuService;
import com.lx.zy.project.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: zy-vue
 * @description: 用户权限（权重）处理
 * @author: Lx
 * @create: 2019-10-20 09:48
 **/
@Component
public class SysPermissionService
{

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysRoleService roleService;

    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUser user)
    {
        Set<String> roles = new HashSet<>();
        //管理员拥有所有权限
        if (user.isAdmin())
        {
            roles.add("*:*:*");
        }
        else
        {
            roles.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
        }
        return roles;
    }

    public Set<String> getRolePermission(SysUser user)
    {
        Set<String> roles = new HashSet<>();
        if (user.isAdmin())
        {
            roles.add("admin");
        }
        else
        {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }
}
